function addLinks() {
    // Check that we're on the right page
    var text = document.URL;
    var priv;
    if (text.indexOf("messages_private.php") >= 0)
        priv = true;
    else if (text.indexOf("messages_alliance.php") >= 0)
        priv = false;
    else 
        return;


    // Search for the messages
    var tables;
    var min;
    var max;
    if (priv) {
        var form1 = document.getElementsByName("form1")[0];
        tables = form1.getElementsByTagName("table");
        min = 0;
        max = tables.length;
    } else {
        var search = document.getElementsByName("search")[0];
        var container = search.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
        tables = container.getElementsByTagName("table");
        min = 1;
        max = tables.length - 2;
    }

    for (var i = max-1; i >= min; i--) {
        try {
            addLinksToMessage (tables[i], priv, i);
        } catch (e) {}
    }	
}

function addLinksToMessage (messageTable, priv, index) {
    var body = messageTable.getElementsByTagName("TBODY")[0];
    body.setAttribute ("name", "po_msg_"+index);
    var newcell = document.createElement ("TD");
    var newrow = document.createElement ("TR");
    newrow.appendChild(newcell);
    
    var msgcelltop = body.getElementsByTagName("TR")[priv?2:3];
    var msgcell = msgcelltop.getElementsByTagName("TD")[0];
    var subj = body.getElementsByTagName("TR")[priv?1:2].getElementsByTagName("B")[0].childNodes[0].nodeValue;

    playercell = body.getElementsByTagName("TR")[0];
    if (playercell.getElementsByTagName("A").length == 0)
            return;
    var player = playercell.getElementsByTagName("A")[0].childNodes[0].nodeValue;	
    
    subj = subj.substr(9);

    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "Re:";
    btn.setAttribute ("onclick", "mysendmsg(event)");
    btn.setAttribute ("to", player);
    btn.setAttribute ("from", player);
    btn.setAttribute ("subj", subj);
    btn.setAttribute ("last", "no");
    btn.setAttribute ("table", "po_msg_"+index);
    btn.setAttribute ("private", priv);
    newcell.appendChild(btn);
    newcell.appendChild (document.createTextNode(" "));

    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "Re: last";
    btn.setAttribute ("onclick", "mysendmsg(event)");
    btn.setAttribute ("to", player);
    btn.setAttribute ("from", player);
    btn.setAttribute ("subj", subj);
    btn.setAttribute ("last", "yes");
    btn.setAttribute ("table", "po_msg_"+index);
    btn.setAttribute ("private", priv);
    newcell.appendChild(btn);
    newcell.appendChild (document.createTextNode(" "));

    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "Fw:";
    btn.setAttribute ("onclick", "mysendmsg(event)");
    btn.setAttribute ("to", "");
    btn.setAttribute ("from", player);
    btn.setAttribute ("subj", subj);
    btn.setAttribute ("last", "no");
    btn.setAttribute ("table", "po_msg_"+index);
    btn.setAttribute ("private", priv);
    newcell.appendChild(btn);
    newcell.appendChild (document.createTextNode(" "));

    var btn = document.createElement("input");
    btn.type = "button";
    btn.value = "Fw: last";
    btn.setAttribute ("onclick", "mysendmsg(event)");
    btn.setAttribute ("to", "");
    btn.setAttribute ("from", player);
    btn.setAttribute ("subj", subj);
    btn.setAttribute ("last", "yes");
    btn.setAttribute ("table", "po_msg_"+index);
    btn.setAttribute ("private", priv);
    newcell.appendChild(btn);
    newcell.appendChild (document.createTextNode(" "));

    body.appendChild (document.createTextNode(""));
    body.appendChild (newrow);
}

function mysendmsg(evt) {
    // Search for the messages
    if (!evt) evt = this;
    else evt = evt.target;

    var player = evt.getAttribute("to");
    var subject = evt.getAttribute("subj");
    var from = evt.getAttribute("from");
    var last = evt.getAttribute("last");
    var index = evt.getAttribute ("table");
    var priv = evt.getAttribute ("private") == "true";
    var txt = "";

    var body = evt.parentNode.parentNode.parentNode;
    var msgcelltop = body.getElementsByTagName("TR")[priv?2:3];
    var msgcell = msgcelltop.getElementsByTagName("TD")[0];
    var subjcell = body.getElementsByTagName("TR")[priv?1:2].getElementsByTagName("B")[0];
    var playercell = body.getElementsByTagName("TR")[0].getElementsByTagName("A")[0];
    var sentcell = playercell.parentNode.parentNode.nextSibling.nextSibling;

    txt = "\n\n---------------------------------------------------------------------\n";
    txt = txt + playercell.previousSibling.nodeValue + playercell.childNodes[0].nodeValue + "\n";
    txt = txt + subjcell.childNodes[0].nodeValue + "\n";
    txt = txt + "Sent: " + sentcell.childNodes[sentcell.childNodes.length-1].nodeValue + "\n\n";
    var skipbr = 0;
    for (var i=0; i<msgcell.childNodes.length; i++) {
        var element = msgcell.childNodes[i];
        if (element.nodeName == "BR") {
            if (skipbr == 0)
                txt = txt + "\n";
            else
                skipbr = 0;
        }
        else if (element.nodeName == "HR")
            break;
        else if (element.nodeName == "A")
            txt = txt + "[url="+element.getAttribute("href")+"]"+element.childNodes[0].nodeValue+"[/url]";
        else if (last == "yes" && element.nodeValue == "--------------------------------------------------------------------- ")
            break;
        else
            txt = txt + element.nodeValue;
    }

    
    localStorage.setItem ("po_msg_reply", txt);
    window.open("sendmsg.php?to="+player+"&subj="+subject,"_blank","width=540,height=434,left=0,top=0");
}

addLinks();


// The New Alliance Messages stuff


function getNewAMs() {
    var newImg = "http://static.pardus.at/img/stdhq/new.jpg";
    var lastAM = getLastAM();
    var ah;
    var f = true;
    
    var a = document.getElementsByTagName("A")
    
    for(i=0;i<a.length;i++) {
        ah = a[i].href;
        if(ah==lastAM) return;
        if(ah.indexOf("repam") > -1) {
            if(f) {
                setLastAM(ah);
                f = false;
            }
            a[i].parentNode.parentNode.parentNode.rows[0].cells[1].innerHTML = "<img src=\"" + newImg + "\"/> " + a[i].parentNode.parentNode.parentNode.rows[0].cells[1].innerHTML;		
        }
    }
}

function setLastAM(lam) {
	localStorage.setItem("po_msg_lastAM", lam);
}

function getLastAM() {
    return localStorage.getItem("po_msg_lastAM");
}


if (document.URL.indexOf ("messages_alliance") != -1) {
    getNewAMs();
}



